﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web;
using System.Data;
namespace LHWebApp
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string clientNo = context.Request.Params["clientNo"];
            string currentService = context.Request.Params["currentService"];
            if (string.IsNullOrEmpty(clientNo))
                clientNo = "0000001";
            string strResult = "[";
            DataTable dt = Services.GetClientVehicleNodeList(currentService, clientNo).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow[] dr = dt.Select("NodeID='" + clientNo + "'");
                strResult = DtTreeToJson(dt, strResult, dr);
            }
            strResult += "]";
            context.Response.ContentType = "text/plain";
            context.Response.Write(strResult.ToString());
            context.Response.End();
        }

        private static string DtTreeToJson(DataTable dt, string strResult, DataRow[] dr)
        {
            if (dr.Length > 0)
            {
                for (int i = 0; i < dr.Length; i++)
                {

                    strResult += "{";
                    strResult += "text:'" + dr[i]["NodeName"] + "',";
                    strResult += "id:'" + dr[i]["NodeID"] + "',";
                    strResult += "NodeType:'" + dr[i]["NodeType"] + "',";
                    strResult += "CardNo:'" + dr[i]["CardNo"] + "',";
                    DataRow[] drChild = dt.Select("NodeParentID='" + dr[i]["NodeID"] + "'");
                    if (drChild.Length > 0)
                    {
                        strResult += "leaf:false,";
                        strResult += "children:[";
                        strResult = DtTreeToJson(dt, strResult, drChild);
                        strResult += "]";
                    }
                    else
                        strResult += "leaf:true";

                    strResult += "}";

                    if (i != dr.Length - 1)
                        strResult += ",";
                }
            }
            return strResult;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
